﻿using System;
using System.Linq;
using System.Linq.Expressions;
using WI.CandidateManager.Common.Data.Enums;

namespace WI.CandidateManager.Business.Objects
{
    /// <summary>
    /// EntityBase business logic.
    /// </summary>
    public abstract class EntityBaseBO : BOBase
    {
        /// <summary>
        /// Gets the name of specified in expression property.
        /// </summary>
        /// <param name="expression">
        /// The expression.
        /// </param>
        /// <returns>
        /// The System.String.
        /// </returns>
        public static string GetPropertyName<TObject, TValue>(Expression<Func<TObject, TValue>> expression)
        {
            return ((MemberExpression)expression.Body).Member.Name;
        }

        /// <summary>
        /// Sets the record with specified ID in deleted state.
        /// </summary>
        /// <param name="id">
        /// The ID.
        /// </param>
        public void DeleteRecord(long id)
        {
            DataContext.EntityBaseSet.Single(r => r.Id == id).State = State.Deleted;
            DataContext.SaveChanges();
        }
    }
}
